package com.itfreer.gather.task;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.itfreer.content.task.TaskEntity;
import com.itfreer.content.task.TaskService;
import com.itfreer.gather.R;
import com.itfreer.ui.activity.BaseActivity;
import com.itfreer.ui.base.TextView;
import com.itfreer.ui.base.checkbox.CheckBox;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 任务管理
 */
public class TaskActivity extends BaseActivity {

    private LinearLayout task_content;
    private LoadTaskHandler loadTaskHandler;

    private TextView task_state_all;
    private View task_state_all_1;

    private TextView task_state_01;
    private View task_state_01_1;

    private TextView task_state_02;
    private View task_state_02_1;

    private TextView task_state_03;
    private View task_state_03_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_activity);

        // 返回
        TextView back =(TextView)findViewById(R.id.taskBack);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        task_content =(LinearLayout)findViewById(R.id.task_content);
        loadTaskHandler = new LoadTaskHandler();

        task_state_all =(TextView)findViewById(R.id.task_state_all);
        task_state_all_1 =(View)findViewById(R.id.task_state_all_1);

        task_state_01 =(TextView)findViewById(R.id.task_state_01);
        task_state_01_1 =(View)findViewById(R.id.task_state_01_1);

        task_state_02 =(TextView)findViewById(R.id.task_state_02);
        task_state_02_1 =(View)findViewById(R.id.task_state_02_1);

        task_state_03 =(TextView)findViewById(R.id.task_state_03);
        task_state_03_1 =(View)findViewById(R.id.task_state_03_1);

        filter(null);

        // 打开时同步一次
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(TaskService.instance.syn()){
                    // 重新加载
                    Message msg = new Message();
                    Bundle b = new Bundle();
                    msg.setData(b);
                    loadTaskHandler.sendMessage(msg);
                }
            }
        }).start();
    }

    private class LoadTaskHandler extends Handler {
        public LoadTaskHandler() {
        }

        // 子类必须重写此方法，接受数据
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            filter(null);
        }
    }

    public void filterAll(View v) {
        filter(null);
    }

    public void filter01(View v) {
        filter("01");
    }

    public void filter02(View v) {
        filter("02");
    }

    public void filter03(View v) {
        filter("03");
    }

    public void filter(String state){
        task_state_all.setTextColor(Color.parseColor("#303059"));
        task_state_all_1.setVisibility(View.GONE);

        task_state_01.setTextColor(Color.parseColor("#303059"));
        task_state_01_1.setVisibility(View.GONE);

        task_state_02.setTextColor(Color.parseColor("#303059"));
        task_state_02_1.setVisibility(View.GONE);

        task_state_03.setTextColor(Color.parseColor("#303059"));
        task_state_03_1.setVisibility(View.GONE);

        if(state==null || state.equals("")){
            task_state_all.setTextColor(Color.parseColor("#ed1a23"));
            task_state_all_1.setVisibility(View.VISIBLE);
        } else if (state.equals("01")){
            task_state_01.setTextColor(Color.parseColor("#ed1a23"));
            task_state_01_1.setVisibility(View.VISIBLE);
        } else if (state.equals("02")){
            task_state_02.setTextColor(Color.parseColor("#ed1a23"));
            task_state_02_1.setVisibility(View.VISIBLE);
        } else if (state.equals("03")){
            task_state_03.setTextColor(Color.parseColor("#ed1a23"));
            task_state_03_1.setVisibility(View.VISIBLE);
        }

        loadTask(state);
    }

    /**
     * 加载信息
     */
    private void loadTask(String state){
        task_content.removeAllViews();
        List<TaskEntity> tasks = TaskService.instance.getEntitys(state);
        if(tasks==null || tasks.size()<=0){
            return;
        }

        for(TaskEntity task : tasks){
            loadMsg(task);
        }
    }

    private void loadMsg(TaskEntity task) {
        final float scale = this. getResources().getDisplayMetrics().density;
        int dp1 = (int) (1 * scale + 0.5f);
        int dp6 = (int) (6 * scale + 0.5f);
        int dp10 = (int) (10 * scale + 0.5f);
        int dp15 = (int) (15 * scale + 0.5f);
        int dp16 = (int) (16 * scale + 0.5f);
        int dp20 = (int) (20 * scale + 0.5f);
        int dp22 = (int) (22 * scale + 0.5f);
        int dp25 = (int) (25 * scale + 0.5f);

        LinearLayout item = new LinearLayout(TaskActivity.this);
        LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        itemParams.setMargins(dp15, dp10, dp15, 0);
        item.setLayoutParams(itemParams);
        item.setOrientation(LinearLayout.VERTICAL);
        item.setBackgroundResource(R.drawable.search_bg);

        TextView title = new TextView(TaskActivity.this);
        LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp22);
        titleParams.setMargins(dp25, dp25,0,0);
        title.setLayoutParams(titleParams);
        title.setGravity(Gravity.CENTER|Gravity.LEFT);
        title.setTextColor(Color.parseColor("#4a4a4a"));
        title.setTextSize(18);
        title.setText(task.getTaskName());
        item.addView(title);

        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TextView pushDate = new TextView(TaskActivity.this);
        LinearLayout.LayoutParams pushDateParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp16);
        pushDateParams.setMargins(dp25, dp6,0,0);
        pushDate.setLayoutParams(pushDateParams);
        pushDate.setGravity(Gravity.CENTER|Gravity.LEFT);
        pushDate.setTextColor(Color.parseColor("#bcbcbc"));
        pushDate.setTextSize(14);
        pushDate.setText("截止时间:" + formate.format(task.getPlanDate()));
        item.addView(pushDate);

        TextView content = new TextView(TaskActivity.this);
        LinearLayout.LayoutParams contentParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        contentParams.setMargins(dp25, dp20, dp25,0);
        content.setLayoutParams(contentParams);
        content.setGravity(Gravity.CENTER|Gravity.LEFT);
        content.setTextColor(Color.parseColor("#e6725c"));
        content.setTextSize(16);
        content.setText(task.getTaskContent());
        item.addView(content);

        View splitView = new View(TaskActivity.this);
        LinearLayout.LayoutParams splitViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp1);
        splitViewParams.setMargins(dp25, dp20, dp25,0);
        splitView.setLayoutParams(splitViewParams);
        splitView.setBackgroundColor(Color.parseColor("#e3e3e3"));
        item.addView(splitView);

        CheckBox option = new CheckBox(TaskActivity.this);
        LinearLayout.LayoutParams optionParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dp22);
        optionParams.setMargins(dp25, dp20, 0, dp20);
        option.setLayoutParams(optionParams);
        option.setGravity(Gravity.CENTER|Gravity.LEFT);
        option.setTextColor(Color.parseColor("#66708d"));
        option.setTextSize(16);
        option.setText("已完成");
        item.addView(option);

        task_content.addView(item);
    }
}
